<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="销售订单名称：">
        <el-input v-model="dataForm.name" placeholder="请输入销售订单名" clearable></el-input>
      </el-form-item>
<!--      <el-form-item label="销售订单编号：">-->
<!--        <el-input v-model="dataForm.code" placeholder="请输入销售订单编号" clearable></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sales:orderheader:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sales:orderheader:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button @click="exportToExcel">导出</el-button>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-button @click="exportToExcel">下载表格</el-button>-->
<!--        <el-button @click="printContent">打印表格</el-button>-->
<!--      </el-form-item>-->
    </el-form>
    <el-table
      :data="dataList"
      border
      id="saleOrderHeaderTable"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        v-model="dataForm.name"
        label="销售订单名">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="code"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="销售订单编号">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="clientId"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="客户ID">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="clientName"
        header-align="center"
        align="center"
        v-model="dataForm.clientName"
        label="客户">
      </el-table-column>
      <el-table-column
        prop="clientTelephone"
        header-align="center"
        align="center"
        label="客户电话">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="staffId"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="职员ID">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="staffName"
        header-align="center"
        align="center"
        v-model="dataForm.staffName"
        label="业务员">
      </el-table-column>
      <el-table-column
        prop="saleDate"
        header-align="center"
        align="center"
        label="销售日期">
      </el-table-column>
      <el-table-column
        prop="deliveryDate"
        header-align="center"
        align="center"
        label="交货日期">
      </el-table-column>

<!--      todo-->
<!--      <el-table-column-->
<!--        prop="storehouseId"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="仓库ID">-->
<!--      </el-table-column>-->
      <el-table-column
        prop="productAddress"
        header-align="center"
        align="center"
        v-model="dataForm.productAddress"
        label="安装地址">
      </el-table-column>
      <el-table-column
        prop="discountRate"
        header-align="center"
        align="center"
        label="优惠率">
      </el-table-column>
      <el-table-column
        prop="discountAmount"
        header-align="center"
        align="center"
        label="优惠金额(元)">
      </el-table-column>
      <el-table-column
        prop="totalAmount"
        header-align="center"
        align="center"
        label="优惠后金额(元)">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        :filters="[{ text: '新增', value: 0 }, { text: '缓冲', value: 1 },
                   { text: '待结算', value: 2 }, { text: '已结算', value: 3 }, { text: '生产中', value: 4 }]"
        :filter-method="filterStatus"
        filter-placement="bottom-end"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small">缓冲</el-tag>
          <el-tag v-if="scope.row.status === 2" size="small" type="warning">待结算</el-tag>
          <el-tag v-if="scope.row.status === 3" size="small" type="danger">已结算</el-tag>
          <el-tag v-if="scope.row.status === 0" size="small" type="success">新增</el-tag>
          <el-tag v-if="scope.row.status === 4" size="small" type="info">生产中</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="warning"
        header-align="center"
        align="center"
        label="单据状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.warning === 1" size="small"  effect="dark" type="danger">异常</el-tag>
          <el-tag v-if="scope.row.warning === 0" size="small" type="success">正常</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">

          <el-button type="text" size="small" :disabled="scope.row.status === 3 "
                     @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" :disabled="scope.row.status === 3 || scope.row.status === 2"
                     @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './orderheader-add-or-update'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          code: '',
          clientName: '',
          staffName: '',
          productAddress: '',
          status: '',
          warning: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sales/orderheader/listAll'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'code': this.dataForm.code,
            'status': this.dataForm.status,
            'warning': this.dataForm.warning
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      filterStatus (value, row) {
        return row.status === value
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sales/orderheader/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 导出Excel
      exportToExcel () {
        let et = XLSX.utils.table_to_book(document.querySelector('#saleOrderHeaderTable')) // 此处传入table的DOM节点
        console.log(11, et)
        let etout = XLSX.write(et, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        try {
          FileSaver.saveAs(new Blob([etout], {
            type: 'application/octet-stream'
          }), '销售订单.xlsx')   // 销售订单.xlsx 为导出的文件名
        } catch (e) {
          console.log(e, etout)
        }
        return etout
      },
      // 打印页面内容
      printContent () {
        console.log(11)
        let wpt = document.querySelector('#saleOrderHeaderTable')
        let newContent = wpt.innerHTML
        let oldContent = document.body.innerHTML

        document.body.innerHTML = newContent
        window.print() // 打印方法
        // window.localtion.reload();
        history.go(0)
        document.body.innerHTML = oldContent
      }
    }
  }
</script>
